package com.zzyl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.Duration;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class testRedis {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void testString(){
        redisTemplate.opsForValue().set("name","zhangsna");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);

        redisTemplate.opsForValue().set("token","abc123",10, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("token","abc123", Duration.ofSeconds(10));

        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "09876", 5, TimeUnit.MINUTES));
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "34567", 5, TimeUnit.MINUTES));

    }
    @Test
    void testHash(){
//        redisTemplate.opsForHash().put("user","name","张三");
//        redisTemplate.opsForHash().put("user","age","30");
        //根据大key和小key获取值
        System.out.println(redisTemplate.opsForHash().get("user", "name"));
        //根据大key获取所有的小key
        System.out.println(redisTemplate.opsForHash().keys("user"));
        //根据大key获取所有的值
        System.out.println(redisTemplate.opsForHash().values("user"));
        //删除小key和值
        redisTemplate.opsForHash().delete("user","name");

    }
    @Test
    void testList(){
        //插入多个值[a,b,c]
       // redisTemplate.opsForList().leftPushAll("mylist","a","b","c");
        //在列表左边插入一个值[d,a,b,c]
    //    redisTemplate.opsForList().rightPush("mylist","G");
        //获取列表中的数据
      //  System.out.println(redisTemplate.opsForList().range("mylist", 0, -1));
        //从左边弹出一个，并获取值，弹出后列表中删除
  //      System.out.println(redisTemplate.opsForList().leftPop("mylist"));
        //获取列表的长度
        System.out.println(redisTemplate.opsForList().size("mylist"));
    }

    @Test
    void testSet(){
        //添加数据
        redisTemplate.opsForSet().add("myset","a","b","c","d");
        redisTemplate.opsForSet().add("myset2","a","b","x","y");
        //获取集合中的所有成员
        System.out.println(redisTemplate.opsForSet().members("myset"));
        //获取集合大小
        System.out.println(redisTemplate.opsForSet().size("myset"));
        //交集
        System.out.println(redisTemplate.opsForSet().intersect("myset", "myset2"));
        //并集
        System.out.println(redisTemplate.opsForSet().union("myset", "myset2"));
    }
    /**
     * 操作有序集合类型的数据
     */
    @Test
    public void testZset(){
        //添加数据
        redisTemplate.opsForZSet().add("myzset","a",1);
        redisTemplate.opsForZSet().add("myzset","b",10);
        redisTemplate.opsForZSet().add("myzset","c",20);

        //获取集合中的所有成员
        Set<String> members = redisTemplate.opsForZSet().range("myzset", 0, -1);
        System.out.println(members);

        //给a成员的分数增加10
        redisTemplate.opsForZSet().incrementScore("myzset","a",10);

        //删除a、b两个成员
        redisTemplate.opsForZSet().remove("myzset","a","b");
    }

    @Test
    void testCommon(){
        //获取所有key
        Set keys = redisTemplate.keys("*");
     //   System.out.println(keys);
        //判断key是否存在
/*        System.out.println(redisTemplate.hasKey("name"));
        //获取key的类型
        DataType type  = redisTemplate.type("myzset");
        System.out.println(type );
        System.out.println(type.name());*/
        //删除key
        redisTemplate.delete("name");
        redisTemplate.delete("myset");
        redisTemplate.delete("myset2");
        redisTemplate.delete("mylist");
    }
}
